package com.taptech.doufu.ui.activity;

import android.os.Bundle;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.cp.CpRankBean;
import com.taptech.doufu.bean.cp.ResponseCpRankList;
import com.taptech.doufu.constant.FragmentLoadType;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.CpListRecyclerAdapter;
import com.taptech.doufu.net.retrofit.api.ApiClient;
import com.taptech.doufu.net.retrofit.api.BaseSubscriber;
import com.taptech.doufu.services.CpService;
import com.taptech.doufu.util.ItemClickSupport;

import java.util.List;

import static com.taptech.doufu.R.id.cp_rank_title;

/**
 * author zhangyongcun
 * 16/9/29
 * CP  行
 */

public class CPRankActivity extends DiaobaoBaseActivity implements View.OnClickListener {
    private static final String TAG = CPRankActivity.class.getSimpleName();

    private TextView mRankTitle;
    private RecyclerView mRanklistRecycle;
    private CpListRecyclerAdapter cpListRecyclerAdapter;
    private List<CpRankBean> rankList;

    private TextView mWeekRank;
    private TextView mMonthRank;
    private TextView mYearRank;
    private ImageView mBack;


    private String rankUrl;
    private String rankTitle;
    private String rankTime = "1";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cp_rank_layout);
        initView();
        rxData();
    }

    private void rxData() {
        if (rankUrl == null) {
            return;
        }
        ApiClient.getInstance().getService().getCpRankList(rankUrl + "&time=" + rankTime)
                .compose(RxJavaHelper.<ResponseCpRankList>observeOnMainThread(this))
                .subscribe(new BaseSubscriber<ResponseCpRankList>() {
                    @Override
                    public void onNext(ResponseCpRankList responseCpRankList) {
                        loadData(responseCpRankList);
                    }
                });
    }

    private void loadData(ResponseCpRankList responseCpRankList) {
        rankList =  responseCpRankList.getData().getTop();
        cpListRecyclerAdapter = new CpListRecyclerAdapter(CPRankActivity.this, rankList);
        mRanklistRecycle.setAdapter(cpListRecyclerAdapter);
        ItemClickSupport.addTo(mRanklistRecycle).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                CpService.enterCpDesActivity(CPRankActivity.this ,Integer.toString(rankList.get(position).getId()));
            }
        });
    }

    private void initView() {
        rankUrl = getIntent().getStringExtra(Constant.URL);
        rankTitle = getIntent().getStringExtra(FragmentLoadType.fragmentTypeName);

        mRankTitle = (TextView) findViewById(cp_rank_title);
        mRanklistRecycle = (RecyclerView) findViewById(R.id.cp_rank_recycle);
        GridLayoutManager mg = new GridLayoutManager(CPRankActivity.this, 1);
        mRanklistRecycle.setLayoutManager(mg);
        mRankTitle.setText(rankTitle);

        mWeekRank = (TextView) findViewById(R.id.cp_rank_tab_week);
        mMonthRank = (TextView) findViewById(R.id.cp_rank_tab_month);
        mYearRank = (TextView) findViewById(R.id.cp_rank_tab_year);
        mBack = (ImageView) findViewById(R.id.cp_rank_list_back);
        mMonthRank.setOnClickListener(this);
        mWeekRank.setOnClickListener(this);
        mYearRank.setOnClickListener(this);
        mBack.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cp_rank_tab_week:
                setTabStyleDAta(1);
                break;
            case R.id.cp_rank_tab_month:
                setTabStyleDAta(2);
                break;
            case R.id.cp_rank_tab_year:
                setTabStyleDAta(3);
                break;
            case R.id.cp_rank_list_back:
                finish();
                break;
        }

    }

    private void setTabStyleDAta(int position) {
        switch (position) {
            case 1:
                setSelect(mWeekRank);
                setNormal(mMonthRank);
                setNormal(mYearRank);
                break;
            case 2:
                setNormal(mWeekRank);
                setSelect(mMonthRank);
                setNormal(mYearRank);
                break;
            case 3:
                setNormal(mWeekRank);
                setNormal(mMonthRank);
                setSelect(mYearRank);
                break;
        }
        rankTime = Integer.toString(position);
        rxData();
    }

    private void setSelect(TextView view) {
        view.setBackgroundResource(R.drawable.owner_red);
        view.setTextColor(getResources().getColor(R.color.white));
    }
    private void setNormal(TextView view) {
        view.setBackground(null);
        view.setTextColor(getResources().getColor(R.color.dark_slate_gray));
    }



}